﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

using LogCS;

using System.Resources;
using System.Diagnostics.CodeAnalysis;

namespace AZPEP_SysRi_Tool
{

    public static class MyError
    {

        public struct DEBUG_RUNNING_INFO
        {
            static int RunNumber;
            static string SourceFilePath;
        }

        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="Code">错误码</param>
        /// <param name="ErrorMessage">错误信息</param>
        /// <param name="err">异常结构体</param>
        /// <param name="WriteLog">是否写入日志，默认为true</param>
        public static void ShowError(
            ErrCode.ErrorCode Code,
            string ErrorMessage = "",
            bool WriteLog = true,
            Exception? err = null
            )
        {
            string Text = "一个致命错误使程序终止任务。如果你是第一次看到本错误信息，试着重启程序并操作，若此错误仍出现，请联系开发者并提供以下错误信息和日志内容：\n\n";
            Text += $"错误代码：{Code}\n错误信息：{ErrorMessage}\n\n";
#if DEBUG
            if(err != null) Text += $"以下是异常结构体信息：\n\n{err.ToString()}\n\n";
#endif
            if (WriteLog)
            {
                LogProtocol.WriteLog.Error(" ！！！ 程序发生错误 ！！！");
                LogProtocol.WriteLog.Error($"错误代码：{Code}");
                LogProtocol.WriteLog.Error($"错误信息：{ErrorMessage}");

                if (err != null) {
                    LogProtocol.WriteLog.Debug($"\n\n！！！！！！！！！！！！！！！！！！！！\n以下是异常结构体的内容：\n\n{err.ToString()}\n\n！！！！！！！！！！！！！！！！！！！！");
                }

                Text += $"此错误信息已写入日志。\n日志路径：{Log.LogCsGetFileFullPath()}";
            }
            else if (!LogProtocol.IsLogServiceEnable() && WriteLog)
            {
                Text += $"未将此错误信息写入日志，因为日志服务未开启。";
            }
            else if (!WriteLog)
            {
                Text += $"未将此错误信息写入日志。";
            }

            MessageBox.Show(
                Text,
                "程序错误",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error
                );
        }
        /// <summary>
        /// 执行紧急退出
        /// </summary>
        /// <param name="SourcesPath">源码文件</param>
        /// <param name="Line">代码行号</param>
        /// <param name="ExitCode">退出码</param>
        [DoesNotReturn]
        private static void RealEmergencyExit
            (
                string? SourcesPath = null,
                int? Line = null,
                int ExitCode = 1
            )
        {
            if (LogProtocol.IsLogServiceEnable())
            {
                LogProtocol.WriteLog.Warning($"程序执行紧急退出任务，退出代码：{ExitCode}");
#if DEBUG
                if (Line != null && SourcesPath != null)
                {
                    LogProtocol.WriteLog.Debug($"发起源码文件：{SourcesPath}，代码行号：{Line}");
                }
#endif
            }
            LogProtocol.StopLogService();
            System.Environment.Exit(ExitCode);
        }

        /// <summary>
        /// 执行紧急退出
        /// </summary>
        /// <param name="ExitCode">退出码</param>

        [DoesNotReturn]
        public static void EmergencyExitEx(int ExitCode = 1)
        {
            // 获取当前执行代码文件路径
            StackTrace st = new StackTrace(1, true);
            StackFrame fram = st.GetFrame(0);
            string source = fram.GetFileName();

            // 获取当前执行代码行号
            StackTrace sta = new StackTrace(1, true);
            StackFrame frama = st.GetFrame(0);
            int lineNum = fram.GetFileLineNumber();

            //退出
            RealEmergencyExit(source, lineNum, ExitCode);
        }



    }
}
